package com.soma.game.controller;

import com.soma.game.model.ResEnv;
import com.soma.game.model.SystemVo;
import com.soma.game.service.GameScoreService;
import com.soma.game.service.GameStaticService;
import com.soma.game.service.SystemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: game
 * @description: 测试
 * @author: hduprince
 * @create: 2018-07-13 17:07
 **/
@RequestMapping("/system")
@RestController
public class SystemController {

    Logger logger = LoggerFactory.getLogger(SystemController.class);

    @Resource
    private GameStaticService gameStaticService;

    @Resource
    private GameScoreService gameScoreService;

    @Resource
    private SystemService systemService;

    @RequestMapping("/clear")
    public ResEnv clear(@RequestParam(value = "type", required = false, defaultValue = "record") String type,
                        @RequestParam("userId") Long uid,
                        @RequestParam("gameId") Long gameId
                        ){
        try {
            if(StringUtils.equals("record", type)){
                gameScoreService.clearRecord(uid, gameId);
            } else {
                gameStaticService.clearScore(uid, gameId);
            }

            return ResEnv.success();
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }


    @RequestMapping("/gamePlayCount")
    public ResEnv gamePlayCount(@RequestParam(value = "start", required = false) Date start,
                                @RequestParam(value = "end", required = false) Date end){
        try {
            List<SystemVo> systemVoList = systemService.gamePlayCount(start, end);
            return ResEnv.success(systemVoList);
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }


    @RequestMapping("/newUserCount")
    public ResEnv newUserCount(@RequestParam(value = "start", required = false) Date start,
                                @RequestParam(value = "end", required = false) Date end){
        try {
            List<SystemVo> systemVoList = systemService.newUserCount(start, end);
            return ResEnv.success(systemVoList);
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }

    @RequestMapping("/dailyUserCount")
    public ResEnv dailyUserCount(@RequestParam(value = "start", required = false) Date start,
                               @RequestParam(value = "end", required = false) Date end){
        try {
            List<SystemVo> systemVoList = systemService.dailyUserCount(start, end);
            return ResEnv.success(systemVoList);
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }


}
